<?php

namespace App\Models;

use App\Enums\AssetSourceType;
use App\Enums\AssetStatus;
use App\Events\AssetCreated;
use BenSampo\Enum\Traits\CastsEnums;

/**
 * @mixin IdeHelperAsset
 */
class Asset extends BaseModel
{
    use CastsEnums;

    protected $dispatchesEvents = [
        'created' => AssetCreated::class,
    ];

    protected $casts = [
        'asset_status' => AssetStatus::class,
        'source_type' => AssetSourceType::class,
    ];

    public function user()
    {
        return $this->belongsTo(FrontendUser::class, 'user_id');
    }

    public function giveUser()
    {
        return $this->belongsTo(self::class, 'source_id')->with(['user']);
    }

    public function asset_key()
    {
        return $this->hasOne(AssetKey::class);
    }

    public function product()
    {
        return $this->belongsTo(Product::class, 'contract_address', 'contract_address');
    }

    protected static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub
        Asset::created(function ($asset) {
            AssetCreated::dispatch($asset);
        });
    }
}
